﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.DirectoryServices;
using Domain.ActiveDirectory.Model;
using System.Text.RegularExpressions;

namespace Domain.ActiveDirectory.Accessor
{
    public class Accessor
    {
        protected Connection _connection;

        public Accessor(Connection connection)
        {
            _connection = connection;
        }

        protected string DetermineSearchString(IEnumerable<string> propertyNames, IEnumerable<string> propertyValues, string objectCategoryAndOrClass)
        {
            StringBuilder filterString = new StringBuilder();
            filterString.Append(objectCategoryAndOrClass);
            if (propertyNames.Count() > 0 && propertyValues.Count() > 0)
            {
                filterString.Append("(|");
                for (int keyIndex = 0; keyIndex < propertyNames.Count(); keyIndex++)
                {
                    for (int valueIndex = 0; valueIndex < propertyValues.Count(); valueIndex++)
                    {
                        string name = propertyNames.ElementAt(keyIndex);
                        string value = propertyValues.ElementAt(valueIndex);
                        string propertyNameValuePair = GetPropertyNameValuePairAddress(name, value);
                        filterString.Append(propertyNameValuePair);
                    }
                }
                filterString.Append(")");
            }
            else
            {
                filterString.Append(")");
            }
            filterString.Append(")");
            return filterString.ToString();
        }

        protected string GetPropertyNameValuePairAddress(string propertyName, string propertyValue)
        {
            StringBuilder result = new StringBuilder();
            result.Append("(");
            if (propertyValue.Contains("*"))
            {
                result.Append(propertyName);
                result.Append("=");
                result.Append(propertyValue);
            }
            else
            {
                result.Append(propertyName);
                result.Append("=*");
                result.Append(propertyValue);
                result.Append("*");
            }
            result.Append(")");
            return result.ToString();
        }

        protected IEnumerable<string> GetCommonNames(ResultPropertyValueCollection results)
        {
            List<string> stringList = new List<string>();
            int propertyCount = results.Count;

            for (int propertyCounter = 0; propertyCounter < propertyCount; propertyCounter++)
            {
                string line = (string)results[propertyCounter];
                var matchcollection = Regex.Matches(line, "CN=([\\w+\\s?\\w+\\s?]+)");
                var match = matchcollection[0];
                string result = match.Groups[1].ToString();
                stringList.Add(result);
            }
            return stringList;
        }
    }
}

